package io.kiki.sba.registry.server.clientInterface.converter.pb;

import io.kiki.sba.registry.common.model.client.pb.DataBoxPb;
import io.kiki.sba.registry.common.model.client.pb.DataBoxesPb;
import io.kiki.sba.registry.core.model.Data;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;


public class DataBoxConvertor {

    public static Data convert2Java(DataBoxPb dataBoxPb) {

        if (dataBoxPb == null) {
            return null;
        } else {
            Data dataJava = new Data();
            dataJava.setData(dataBoxPb.getData());
            return dataJava;
        }
    }

    public static DataBoxPb convert2Pb(Data dataJava) {

        if (dataJava == null) {
            return null;
        } else {
            DataBoxPb.Builder builder = DataBoxPb.newBuilder();

            if (dataJava.getData() != null) {
                builder.setData(dataJava.getData());
            }
            return builder.build();
        }
    }

    public static List<Data> convertBoxes2Javas(List<DataBoxPb> dataBoxPbs) {

        List<Data> result = new ArrayList<>();
        if (dataBoxPbs == null) {
            return null;
        } else {

            for (DataBoxPb dataBoxPb : dataBoxPbs) {
                result.add(convert2Java(dataBoxPb));
            }

            return result;
        }
    }

    public static List<DataBoxPb> convert2Pbs(List<Data> dataJavas) {

        List<DataBoxPb> result = new ArrayList<>();
        if (dataJavas == null) {
            return null;
        } else {

            for (Data dataJava : dataJavas) {
                result.add(convert2Pb(dataJava));
            }

            return result;
        }
    }

    public static List<Data> convertBoxes2Javas(DataBoxesPb dataBoxesPb) {
        if (dataBoxesPb == null) {
            return null;
        } else {

            return convertBoxes2Javas(dataBoxesPb.getDataList());
        }
    }

    public static Map<String, List<Data>> convert2JavaMaps(Map<String, DataBoxesPb> mapPb) {
        if (mapPb == null) {
            return null;
        } else {
            Map<String, List<Data>> mapJava = new HashMap<>();

            for (Entry<String, DataBoxesPb> entry : mapPb.entrySet()) {
                mapJava.put(entry.getKey(), convertBoxes2Javas(entry.getValue()));
            }

            return mapJava;
        }
    }

    private static DataBoxesPb convertBoxes2Pbs(List<Data> data) {
        if (data == null) {
            return null;
        }
        return DataBoxesPb.newBuilder().addAllData(convert2Pbs(data)).build();
    }

    public static Map<String, DataBoxesPb> convert2PbMaps(Map<String, List<Data>> mapJava) {
        if (null == mapJava) {
            return null;
        }

        Map<String, DataBoxesPb> mapPb = new HashMap<>();

        for (Entry<String, List<Data>> entry : mapJava.entrySet()) {
            mapPb.put(entry.getKey(), convertBoxes2Pbs(entry.getValue()));
        }
        return mapPb;
    }
}
